import imageio.v2 as imageio
import os  

# 文件夹路径  
#subfolder = 'scatter_plots_nparticle_{:d}_ntime_{:d}_ndiag_{:d}'.format(nparticle,ntime,ndiag)  
subfolder = './RB_plot/n_pcolor'   
# 设置GIF动画的帧率（例如，每秒10帧）  
fps = 10  
  
# 获取子文件夹中所有PNG文件的路径  
image_paths = [os.path.join(os.getcwd(), subfolder, f) for f in os.listdir(os.path.join(os.getcwd(), subfolder)) if f.endswith('.png')]  
  
# 排序文件路径，确保它们按数字顺序排列  
image_paths.sort(key=lambda x: int(os.path.splitext(x)[0].split('_')[-1]))  
  
# 使用imageio保存为GIF  
with imageio.get_writer(os.path.join(os.getcwd(), 'n_pcolor.gif'), mode='I', fps=fps) as writer:  
    for path in image_paths:  
        image = imageio.imread(path)  
        writer.append_data(image)  
  
print("GIF animation created successfully!")